package com.sds.sweng.swat.miplatform.ui.design.utils;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;

import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformComponent;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformContainer;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.SubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.commands.CopySubAreaCommand;

public class EditPolicyUtils {

	public static Command createCopySubAreaCommand(CreateRequest request, EditPart host) {
		MiplatformComponent newObject = (MiplatformComponent) request.getNewObject();

		if (!(newObject instanceof SubArea))
			return null;

		if (newObject.getClass() != host.getModel().getClass())
			return null;

		SubArea hostSubArea = (SubArea) host.getModel();
		SubArea newSubArea = (SubArea) newObject;

		if (newSubArea.isFixed()) {
			if (hostSubArea.getWidth() != newSubArea.getWidth())
				return null;
		} else {
			if (hostSubArea.getWidth() < newSubArea.getRequireWidth())
				return null;
		}

		return new CopySubAreaCommand((SubArea) newObject, (SubArea) host.getModel(), (MiplatformContainer) host.getParent().getModel());
	}
}
